package getCourseMapping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class GetCourseList {

	//向 http://librarywap.koolearn.com/getProductInfo?productId= 发送GET请求，范围从
	
	private static String baseURLString = "http://librarywap.koolearn.com/getProductInfo?productId=";
	
	protected static Map<Integer, String> courseMapping = new HashMap<>();
	
	public static String getInfo(Integer courseID) {
		StringBuilder sb = new StringBuilder();
		sb.append(baseURLString).append(courseID);
        String path = sb.toString(); 
        //1, 取得URL对象
        URL url = null;
		
        try {
			url = new URL(path);
		} catch (MalformedURLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
        
        //2, 建立连接
        HttpURLConnection conn=null;
		try {
			conn = (HttpURLConnection) url.openConnection();
		} catch (IOException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		} 
        //3, 请求方式为GET
        
        try {
			conn.setRequestMethod("GET");
		} catch (ProtocolException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} 
        
        
        //4, 允许输入 
        conn.setDoInput(true); 
        //5, 获取连接的输入流（输入主机返回的数据），并放在字符串中 
        BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		
        String str = null;
		try {
			str = br.readLine();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
        return  str;
	}
	
	public static void process() {
		
		for(int i = 50000;i< 60000 ;i++) {
			String result = getInfo(i);
			JSONObject jObject = JSONObject.parseObject(result);
			String cName = jObject.getJSONObject("data").getString("name");
			if(cName!=null) {
				if(cName.contains("试用")) continue;
				//System.err.println(cName);
				courseMapping.put(i, cName);
			}
		}
	}
		
	public static void main(String[] args) throws IOException {
		// TODO 自动生成的方法存根
		//process();
		
		//System.out.println(courseMapping);
		
		Integer startInteger = 90000;
		
		for(int i=0;i<10;i++) {
			Integer currentStartId = startInteger + i*1000;
			Thread thread = new MyThread();
			thread.setName(currentStartId.toString());
			thread.start();
		}
		
	}

}
